<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
//引入数据库类
use DB;

class CateController extends Controller
{
    //后台分类添加
    public function getAdd()
    {	
    	//查询所有的分类数据
    	// $cates = DB::table('cates')->get();

    	//获取格式化分类数据
    	$cates = self::getCates();

    	//显示添加页面
    	return view('admin.cate.add',['cates'=>$cates]);
    }

    //后台分类添加
    public function postInsert(Request $request)
    {
    	// dd($request->all());
    	//拼接数据
    	$data = $request->only(['pid','name']);
    	//判断是否为顶级分类
    	if($data['pid'] == 0){
    		$data['path'] = 0;
    	}else{
    		//先查父级的path路径
    		$res = DB::table('cates')->where('id',$data['pid'])->first();
    		$data['path'] = $res->path.','.$data['pid'];
    	}
    	// dd($data);
    	//执行数据添加
    	$res = DB::table('cates')->insert($data);
    	//判断
    	if($res){
    		return redirect('/admin/cate/index')->with('success','添加成功');
    	}else{
    		return back()->with('error','数据添加失败');
    	}
    }

    //分类列表
    public function getIndex(Request $request)
    {
    	//查询所有分类数据
    	// $cates = DB::table('cates')->get();
    	//select *,concat(path,",",id) as paths from cates order by paths;

    	// $cates = DB::table('cates')
    	// 	->select(DB::raw('*,concat(path,",",id) as paths'))
    	// 	->orderBy('paths')
    	// 	->get();

    	// $cates = DB::select('select *,concat(path,",",id) as paths from cates order by paths');
    	// //遍历
    	// foreach ($cates as $k=>$v){
    	// 	//获取长度
    	// 	$arr = explode(',',$v->path);
    	// 	$len = count($arr) - 1;
    	// 	$v->name = str_repeat('|----',$len).$v->name;
    	// }

    	// 获取格式化分类数据
    	// $cates = self::getCates();

    	//获取每页显示几条
		$num = $request->input('num',10);

		//查询数据库 使用分页
		if($request->input('keywords')){
			$cates = DB::table('cates')
			->select(DB::raw('*,concat(path,",",id) as paths'))
			->where('name','like','%'.$request->input('keywords').'%')
			->orderBy('paths')
			->paginate($num);
		}else{
			$cates = DB::table('cates')
			->select(DB::raw('*,concat(path,",",id) as paths'))
			->orderBy('paths')
			->paginate($num);
		}

    	// dd($cates);
    	//遍历
    	foreach ($cates as $k=>$v){
    		//获取长度
    		$arr = explode(',',$v->path);
    		$len = count($arr) - 1;
    		$v->name = str_repeat('|----',$len).$v->name;
    	}

    	//解析模板
    	return view('admin.cate.index',['cates'=>$cates,'request'=>$request->all()]);
    }

    //格式化  分类列表数据
    public static function getCates()
    {	
    	$cates = DB::select('select *,concat(path,",",id) as paths from cates order by paths');
    	//遍历
    	foreach ($cates as $k=>$v){
    		//获取长度
    		$arr = explode(',',$v->path);
    		$len = count($arr) - 1;
    		$v->name = str_repeat('|----',$len).$v->name;
    	}
    	//返回结果
    	return $cates;
    }


    // -------ajax删除操作------------

    // public function postAjaxdel(Request $request)
    // {
    // 	// dd($request->input('id'));
    // 	$id = $request->input('id');
    // 	//查询当前类下是否有子类
    // 	$res = DB::table('cates')->where('pid',$id)->count();
    // 	// dd($res);
    // 	//判断
    // 	if($res > 0){
    // 		//分类下有子类
    // 		echo 1;
    // 	}else{
    // 		//执行id删除数据
    // 		$ress = DB::table('cates')->where('id',$id)->delete();
    // 		if($ress){
    // 			//删除成功
    // 			echo 2;
    // 		}else{
    // 			//删除失败
    // 			echo 0;
    // 		}
    // 	}
    // }

    //删除操作
    public function getDelete(Request $request)
    {
    	// dd($request->input('id'));
    	$id = $request->input('id');
    	//查询当前类是是否有子类
    	$res = DB::table('cates')->where('pid',$id)->count();
    	//判断
    	if($res > 0 ){
    		//直接返回 并提示
    		return back()->with('error','该分类下有子类,不允许删除哦~');
    	}
    	//执行删除
    	$ress = DB::table('cates')->where('id',$id)->delete();
    	// dd($ress);
    	//判断
    	if($ress){
    		return redirect('/admin/cate/index')->with('success','删除成功');
    	}else{
    		return back()->with('error','数据删除失败');
    	}
    }

    //显示分类修改页面
    public function getEdit(Request $request)
    {
    	// dd($request->input('id'));
    	//接收修改id
    	$id = $request->input('id');
    	//查询当前类是是否有子类
    	$res = DB::table('cates')->where('pid',$id)->count();
    	//判断
    	// dd($res);
    	//通过id查询 修改数据
    	$cate = DB::table('cates')->where('id',$id)->first();
    
    	// 获取格式化分类数据
    	$cates = self::getCates();
    	// dd($cates);

    	if($res > 0 ){
    		//该分类下有子类
    		return view('admin.cate.edit',['cate'=>$cate]);
    	}else{
    		//设置一个标识  如果该分类下没有子类  才让修改分类
    		$cate->test = 1;
    		// dd($cate);
    		return view('admin.cate.edit',['cate'=>$cate,'cates'=>$cates]);
    	}
    }

    //分类修改
    public function postUpdate(Request $request)
    {	
    	$pid = $request->input('pid');
    	// dd($pid);
    	//判断是否有pid
    	if(isset($pid) ){
	    	$data = $request->except(['_token','id']);

	    	//判断是否为顶级分类
	    	if($data['pid'] == 0){
	    		$data['path'] = 0;
	    	}else{
	    		//先查父级的path路径
	    		$res = DB::table('cates')->where('id',$data['pid'])->first();
	    		$data['path'] = $res->path.','.$data['pid'];
	    	}
	    	// dd($data);
    	}else{
    		$data = $request->except(['_token','id']);
    		// dd($data);
    	}

    	//获取修改id
    	$id = $request->input('id');
    	//执行数据修改
    	$res = DB::table('cates')->where('id',$id)->update($data);
    	//判断
		if($res){
			return redirect('/admin/cate/index')->with('success','修改成功');
		}else{
			return back()->with('error','数据修改失败');
		}
    }
    

    //根据pid获取pid下所有子分类   递归查询数据库(效率低)
    public static function getCatesByPid($pid)
    {
    	//获取$pid下面的所有的子类
    	$res = DB::table('cates')->where('pid',$pid)->get();

    	// dd($res);
    	$data = [];
    	//遍历
    	foreach($res as $k=>$v){
    		$v->sub = self::getCatesByPid($v->id);
    		$data[] = $v;
    	}
    	//返回
    	return $data;
    }

    //测试 
    public function getCeshi()
    {	
    	//递归查询数据库  进行格式化数据
    	// $res = self::getCatesByPid(0);
    	// dd($res);

    	//调用方法查询所有分类
    	$arr = self::getAllCates();
    	// dd($arr);
    	//递归方法
    	$res = self::getCatesByPidArr($arr,0);
    	dd($res);

    }

    ///-------------------递归查询优化---------------------
    //查询所有的分类数据
    public static function getAllCates()
    {
    	return DB::table('cates')->get();
    }

     //根据pid获取pid下所有子分类   (效率高)
    public static function getCatesByPidArr($cates,$pid)
    {
    	$data = [];
    	//遍历
    	foreach($cates as $k=>$v){
    		if($v->pid == $pid){
    			$v->sub = self::getCatesByPidArr($cates,$v->id);
    			$data[] = $v;
    		}
    	}
    	//返回
    	return $data;
    }

}
